﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ZenSystemService.Api.Business;
using ZenSystemService.Api.Common.Auth;
using ZenSystemService.Api.DTOs;
using ZenSystemService.Api.DTOs.TrainingItem;
using ZenSystemService.Api.Entities;
using ZenSystemService.Api.Services;

namespace ZenSystemService.Api.Controllers
{
    /// <summary>
    ///训练项目接口
    /// </summary>
    [Route("api/trainingItems")]
    [ApiController]
    public class TrainingItemController : ControllerBase
    {
        private readonly InstitutionTrainingItemService _trainingItemService;

        public TrainingItemController(InstitutionTrainingItemService trainingItemService)
        {
            _trainingItemService = trainingItemService;
        }

        /// <summary>
        /// 得到所有训练项目
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ReturnObjectT<IEnumerable<TrainingItemQueryResultDTO>>> GetAllTrainingItems(ReceiveObject receiveObject)
        {
            ReturnObjectT<IEnumerable<TrainingItemQueryResultDTO>> _returnObject = new ReturnObjectT<IEnumerable<TrainingItemQueryResultDTO>>();


            var TrainingItems = await _trainingItemService.GetAllTrainingItemsAsync(receiveObject);
            _returnObject.Code = 1;
            _returnObject.Data = TrainingItems;
            return _returnObject;
            // return Ok(TrainingItems);
        }

        /// <summary>
        /// 根据ID 获取训练项目实体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("GetTrainingItemById")]
        public async Task<ReturnObjectT<TrainingItemQueryResultDTO>> GetTrainingItemById(ReceiveObjectT<int> _receiveObjectT)
        {
            ReturnObjectT<TrainingItemQueryResultDTO> _returnObject = new ReturnObjectT<TrainingItemQueryResultDTO>();

            int id = _receiveObjectT.Data;
            TrainingItemQueryResultDTO trainingItem = null;// await _trainingItemService.GetTrainingItemByIdAsync(id);
            if (trainingItem == null)
            {
                //return NotFound();
                _returnObject.Code = 0;
                _returnObject.ErrorMsg = "获取训练项目信息失败";
            }
            else
            {
                _returnObject.Code = 1;
                _returnObject.Data = trainingItem;
            }
            return _returnObject;
            // return Ok(TrainingItem);
        }
        /// <summary>
        /// 创建训练项目信息
        /// </summary>
        /// <param name="trainingItem"></param>
        /// <returns></returns>
        [HttpPost("CreateTrainingItem")]
        public async Task<IActionResult> CreateTrainingItem(TrainingItemCreateRequestDTO trainingItem)
        {
            TrainingItemQueryResultDTO trainingItemResult = await _trainingItemService.CreateTrainingItemAsync(trainingItem);
            return CreatedAtAction(nameof(GetTrainingItemById),
                new { id = trainingItemResult.TrainingItemId }, trainingItemResult);
        }

        /// <summary>
        /// 更新训练项目信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="trainingItem"></param>
        /// <returns></returns>
        [HttpPost("UpdateTrainingItem")]
        public async Task<IActionResult> UpdateTrainingItem(TrainingItemUpdateRequestDTO req)
        {
            var trainingItem = ""; // await _trainingItemService.UpdateTrainingItemAsync(req);
            return Ok(trainingItem);
        }
    }
}